import { usePermissionStore } from '@/store/modules/permission';
import { Router } from 'vue-router';
import { constantsBefore } from '../constants';

const whiteList = ['/not-found', '/login'];

export function createPermissionGuard(router: Router) {
  router.beforeEach(async (to, from, next) => {
    console.log('createPermissionGuard');
    if(whiteList.includes(to.path)) {
      next();
      return;
    }
    const permissionStore = usePermissionStore();
    if(!permissionStore.authRoutes.length) {
      await permissionStore.generateRoutes();
      [...permissionStore.authRoutes, ...constantsBefore].map((route) => {
        router.addRoute(route);
      });
      next(to);
    }
    next();
  });
}
